package com.tom.demo.service.product.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tom.demo.common.entity.Trademark;
import com.tom.demo.common.response.Result;
import com.tom.demo.mapper.TrademarkMapper;
import com.tom.demo.service.product.TrademarkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Slf4j
@Service
public class TrademarkServiceImpl implements TrademarkService {

    @Resource
    private Snowflake snowflake;

    @Resource
    private TrademarkMapper trademarkMapper;

    @Override
    public Result list(Integer page, Integer pageSize) {
        LambdaQueryWrapper<Trademark> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Trademark::getCreateTime);
        Page<Trademark> IPage = new Page<>(page,pageSize);
        Page<Trademark> trademarkPage = trademarkMapper.selectPage(IPage, lambdaQueryWrapper);
        return Result.success(trademarkPage);
    }

    @Override
    public Result save(Trademark trademark) {
        trademark.setId(snowflake.nextIdStr());
        int row = trademarkMapper.insert(trademark);
        return row > 0 ? Result.success():Result.error("新增成功");
    }

    @Override
    public Result update(Trademark trademark) {
        String id = trademark.getId();
        if(!StringUtils.hasText(id)){
            return Result.error("id不能为空");
        }
        int row = trademarkMapper.updateById(trademark);
        return row > 0 ? Result.success():Result.error("修改失败");
    }

    @Override
    public Result delete(String id) {
        int row = trademarkMapper.deleteById(id);
        return row > 0 ? Result.success():Result.error("删除失败");
    }

    @Override
    public Result findById(String id) {
        Trademark trademark = trademarkMapper.selectById(id);
        return Result.success(trademark);
    }
}
